package com.tedu.weibo.controller;

import com.tedu.weibo.mapper.WeiboMapper;
import com.tedu.weibo.pojo.dto.WeiboDTO;
import com.tedu.weibo.pojo.entity.User;
import com.tedu.weibo.pojo.entity.Weibo;
import com.tedu.weibo.pojo.vo.WeiboDetailVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import javax.xml.crypto.Data;
import java.sql.SQLOutput;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/weibo")
@Api(tags = "微博模块")
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;
    @PostMapping("/insert")
    @ApiOperation("发表微博")
    public int insert(@RequestBody WeiboDTO weiboDTO,@ApiIgnore HttpSession session){
        System.out.println("准备发布微博...");
        System.out.println(weiboDTO);
        User user = (User) session.getAttribute("user");
        if (user == null){//未登录
            return 2;
        }
        //已登录,发表微博
        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboDTO, weibo);
        //System.out.println(weibo);
        weibo.setCreated(new Date());
        weibo.setUserId(user.getId());
        weiboMapper.insertWeibo(weibo);
        return 1;
    }

    @GetMapping("selectIndex")
    @ApiOperation("获取微博首页")
    public List<WeiboDetailVO> findIndex(){
        return weiboMapper.findIndex();
    }

    @GetMapping("selectById")
    @ApiOperation("查看指定微博")
    @ApiImplicitParam(name = "id",value = "微博id",required = true,example = "1",dataType = "int")
    public WeiboDetailVO selectById(Integer id){
        return weiboMapper.findById(id);
    }
}
